home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Bavarian / Bavarian #022 (19xx)(APS Electronic).zip / Bavarian #022 (19xx)(APS Electronic).adf / console.mod < prev    next >
Text File  |  1986-11-29  |  9KB  |  253 lines

  1. IMPLEMENTATION MODULE Console;
  2.  
  3. (**********************************************************************
  4. ***************           Written by Ed Bartz           ***************
  5. ***************           Copyright  5/21/87            ***************
  6. ***************    This program may be redistributed    ***************
  7. ***************    or modified as long as these         ***************
  8. ***************    notices and all other references     ***************
  9. ***************    to the author remain intack.         ***************
  10. ***************    Also this may not be used for        ***************
  11. ***************    profit by anyone without the         ***************
  12. ***************    express permission of the author.    ***************
  13. **********************************************************************)
  14.  
  15.   FROM IO IMPORT IOStdReqPtr, DoIO, SendIO, CmdRead, CmdWrite, AbortIO;
  16.   FROM PortUtils IMPORT CreatePort,DeletePort,CreateStdIO,DeleteStdIO;
  17.   FROM Ports IMPORT WaitPort, MsgPortPtr, MessagePtr, GetMsg;
  18.   FROM Intuition IMPORT WindowPtr;
  19.   FROM Devices IMPORT OpenDevice, CloseDevice;
  20.   FROM SYSTEM IMPORT ADR, NULL;
  21.  
  22.   (*+,+*)
  23.  
  24. PROCEDURE QueueRead(VAR Rport: Conport);
  25.         (* queue up a read request to a console, show where to
  26.          * put the character when ready to be returned.  Most
  27.          * efficient if this is called right after console is
  28.          * opened *)
  29.  
  30.         BEGIN
  31.                 Rport.IO^.ioReq.ioCommand := CmdRead;
  32.                 Rport.IO^.ioData := ADR(Rport.buf[0]);
  33.                 Rport.IO^.ioLength := 1;
  34.                 SendIO(Rport.IO^.ioReq);
  35.         END QueueRead;
  36.  
  37. PROCEDURE OpenWRConsole(VAR Wport, Rport: Conport; w: WindowPtr):BOOLEAN;
  38. (* Open a console device *)
  39.          VAR
  40.            i : INTEGER;
  41.            error :LONGCARD;
  42.            err : BOOLEAN;
  43.            c : CHAR;
  44.  
  45.          BEGIN
  46.                 error:=1;
  47.                 Wport.msg := CreatePort("my.con.write",0);
  48.                 Wport.IO := CreateStdIO(Wport.msg);
  49.                 Rport.msg := CreatePort("my.con.read",0);
  50.                 Rport.IO := CreateStdIO(Rport.msg);
  51. IF NOT((Wport.msg=NULL)OR(Wport.IO=NULL)OR(Rport.msg=NULL)OR(Rport.IO=NULL))THEN
  52.                 Wport.IO^.ioData:= w;
  53.                 Wport.IO^.ioLength := SIZE(w^);
  54.                 error := OpenDevice("console.device", 0, Wport.IO, 0);
  55.                 Rport.IO^.ioReq.ioDevice := Wport.IO^.ioReq.ioDevice;
  56.                 Rport.IO^.ioReq.ioUnit := Wport.IO^.ioReq.ioUnit;
  57.                         (* clone required parts of the request *)
  58.              END;
  59.              IF error>0 THEN err:=FALSE;ELSE err:=TRUE;END;
  60.              QueueRead(Rport);
  61.              FOR i := 0 TO 79 DO
  62.                Rport.buf[i]:= 0C;
  63.                Wport.buf[i]:= 0C;
  64.              END;
  65.              RETURN err
  66.          END OpenWRConsole;
  67.  
  68. PROCEDURE OpenWConsole(VAR Wport: Conport; w: WindowPtr):BOOLEAN;
  69. (* Open a console device *)
  70.          VAR
  71.            i : INTEGER;
  72.            error :LONGCARD;
  73.            err : BOOLEAN;
  74.            c : CHAR;
  75.  
  76.          BEGIN
  77.                 error:=1;
  78.                 Wport.msg := CreatePort("my.con.write",0);
  79.                 Wport.IO := CreateStdIO(Wport.msg);
  80.                 IF NOT((Wport.msg=NULL)OR(Wport.IO=NULL))THEN
  81.                   Wport.IO^.ioData:= w;
  82.                   Wport.IO^.ioLength := SIZE(w^);
  83.                   error := OpenDevice("console.device", 0, Wport.IO, 0);
  84.                 END;
  85.                IF error>0 THEN err:=FALSE;ELSE err:=TRUE;END;
  86.                FOR i := 0 TO 79 DO
  87.                  Wport.buf[i]:= 0C;
  88.                END;
  89.                RETURN err
  90.          END OpenWConsole;
  91.  
  92. PROCEDURE OpenRConsole(VAR Rport: Conport; w: WindowPtr):BOOLEAN;
  93. (* Open a console device *)
  94.          VAR
  95.            i : INTEGER;
  96.            error :LONGCARD;
  97.            err : BOOLEAN;
  98.            c : CHAR;
  99.  
  100.          BEGIN
  101.                 error:=1;
  102.                 Rport.msg := CreatePort("my.con.read",0);
  103.                 Rport.IO := CreateStdIO(Rport.msg);
  104.                 IF NOT((Rport.msg=NULL)OR(Rport.IO=NULL))THEN
  105.                   Rport.IO^.ioData:= w;
  106.                   Rport.IO^.ioLength := SIZE(w^);
  107.                   error := OpenDevice("console.device", 0, Rport.IO, 0);
  108.                 END;
  109.                 IF error>0 THEN err:=FALSE;ELSE err:=TRUE;END;
  110.                 IF err THEN QueueRead(Rport); END;
  111.                 FOR i := 0 TO 79 DO
  112.                   Rport.buf[i]:= 0C;
  113.                 END;
  114.                 RETURN err
  115.          END OpenRConsole;
  116.  
  117.  PROCEDURE CloseWRConsole(Wport, Rport: Conport);
  118.  
  119.       BEGIN
  120.         AbortIO(Rport.IO^.ioReq);
  121.         CloseDevice(Wport.IO);
  122.         DeleteStdIO(Wport.IO);
  123.         DeleteStdIO(Rport.IO);
  124.         DeletePort(Wport.msg);
  125.         DeletePort(Rport.msg);
  126.       END CloseWRConsole;
  127.  
  128.  PROCEDURE CloseWConsole(Wport: Conport);
  129.  
  130.       BEGIN
  131.         CloseDevice(Wport.IO);
  132.         DeleteStdIO(Wport.IO);
  133.         DeletePort(Wport.msg);
  134.       END CloseWConsole;
  135.  
  136.  PROCEDURE CloseRConsole(Rport: Conport);
  137.  
  138.       BEGIN
  139.         AbortIO(Rport.IO^.ioReq);
  140.         CloseDevice(Rport.IO);
  141.         DeleteStdIO(Rport.IO);
  142.         DeletePort(Rport.msg);
  143.       END CloseRConsole;
  144.  
  145. PROCEDURE PutChar(Wport: Conport; c: CHAR);
  146. (* Output a single character to a specified console *)
  147.  
  148.         VAR
  149.          i : LONGINT;
  150.  
  151.         BEGIN
  152.                 Wport.IO^.ioReq.ioCommand := CmdWrite;
  153.                 Wport.IO^.ioData := ADR(c);
  154.                 Wport.IO^.ioLength := 1;
  155.                 i:=DoIO(Wport.IO^.ioReq);
  156.                 (* command works because DoIO blocks until command is
  157.                  * done (otherwise pointer to the character could become
  158.                  * invalid in the meantime).
  159.                  *)
  160.         END PutChar;
  161.  
  162. PROCEDURE Writestr(Wport: Conport; VAR s: ARRAY OF CHAR; len: LONGINT);
  163. (* Output a stream of known length to a console *)
  164.         VAR
  165.          i : LONGINT;
  166.  
  167.         BEGIN
  168.                 Wport.IO^.ioReq.ioCommand := CmdWrite;
  169.                 Wport.IO^.ioData := ADR(s);
  170.                 Wport.IO^.ioLength := len;
  171.                 i:=DoIO(Wport.IO^.ioReq);
  172.                 (* command works because DoIO blocks until command is
  173.                  * done (otherwise pointer to string could become
  174.                  * invalid in the meantime).
  175.                  *)
  176.         END Writestr;
  177.  
  178. PROCEDURE PutStr(Wport: Conport; VAR s: ARRAY OF CHAR);
  179. (* Output a NULL-terminated string of characters to a console *)
  180.  
  181.         VAR
  182.          i : LONGINT;
  183.  
  184.         BEGIN
  185.                 Wport.IO^.ioReq.ioCommand := CmdWrite;
  186.                 Wport.IO^.ioData := ADR(s);
  187.                 Wport.IO^.ioLength := MAX (LONGCARD);
  188.                                           (* tells console to end when it
  189.                                            * sees a terminating zero on
  190.                                            * the string. *)
  191.                 i:=DoIO(Wport.IO^.ioReq);
  192.         END PutStr;
  193.         
  194. PROCEDURE MayGetChar(VAR Rport: Conport; VAR c: CHAR): BOOLEAN;
  195.         (* see if there is a character to read.  If none, don't wait, 
  196.          * come back with a value of FALSE *)
  197.  
  198.  
  199.         BEGIN
  200.                 IF (GetMsg(Rport.msg)=MessagePtr(0))THEN RETURN FALSE;
  201.                 ELSE
  202.                 QueueRead(Rport);
  203.                 c:= Rport.buf[0];
  204.                 RETURN TRUE;
  205.                 END;
  206.         END MayGetChar;
  207.  
  208. PROCEDURE GetChar(VAR Rport: Conport; VAR c: CHAR);
  209.         (* go and get a character; put the task to sleep if
  210.           there isn't one present *)
  211.         VAR
  212.           i : MessagePtr;
  213.         BEGIN
  214.                 WHILE GetMsg(Rport.msg) = MessagePtr(0) DO
  215.                   IF WaitPort(Rport.msg) = MessagePtr(0) THEN END;
  216.                 END;
  217.                 QueueRead(Rport);
  218.                 c:= Rport.buf[0];
  219.         END GetChar;
  220.  
  221. PROCEDURE GetStr(VAR Rport, Wport: Conport; VAR s: ARRAY OF CHAR): BOOLEAN;
  222.      VAR
  223.        i,j : INTEGER;
  224.        str : ARRAY [0..80] OF CHAR;
  225.        c : CHAR;
  226.  
  227.      BEGIN
  228.         i:=0;
  229.         c:=' ';
  230.         QueueRead(Rport);
  231.         WHILE ((i<79)AND(c#15C)) DO
  232.           GetChar(Rport,c);
  233.           s[i] := c;
  234.           IF c #15C THEN
  235.             IF (c=10C)OR(c=177C) THEN 
  236.               c:=10C;
  237.               i:=i-2;
  238.               IF i<(-1) THEN i:=(-1);END;
  239.             END;
  240.             i:=i+1;
  241.             PutChar(Wport,c);
  242.           END;
  243.         END;
  244.         QueueRead(Rport);
  245.         IF i#0 THEN
  246.           s[i]:=0C;
  247.           RETURN TRUE;
  248.         END;
  249.         RETURN FALSE;
  250.      END GetStr;
  251.  
  252. END Console.
  253.